package com.hjm.project.bankExpenditure.bankExpenditure.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.ExportExcelUtil;
import com.hjm.framework.web.domain.AjaxResult;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.bankExpenditure.bankExpenditure.mapper.BankExpenditureMapper;
import com.hjm.project.bankExpenditure.bankExpenditure.domain.BankExpenditure;
import com.hjm.project.bankExpenditure.bankExpenditure.service.IBankExpenditureService;
import com.hjm.common.utils.text.Convert;

/**
 * 消费Service业务层处理
 *
 * @author LOVE
 * @date 2020-11-27
 */
@Service
public class BankExpenditureServiceImpl implements IBankExpenditureService {
    @Autowired
    private BankExpenditureMapper bankExpenditureMapper;

    /**
     * 查询消费
     *
     * @param serial 消费ID
     * @return 消费
     */
    @Override
    public BankExpenditure selectBankExpenditureById(Long serial) {
        return bankExpenditureMapper.selectBankExpenditureById(serial);
    }

    /**
     * 查询消费列表
     *
     * @param bankExpenditure 消费
     * @return 消费
     */
    @Override
    public List<BankExpenditure> selectBankExpenditureList(BankExpenditure bankExpenditure) {
        return bankExpenditureMapper.selectBankExpenditureList(bankExpenditure);
    }

    /**
     * 查询消费汇总统计
     *
     * @param bankExpenditure 消费
     * @return 消费
     */
    @Override
    public Map<String,Object> selectBankExpenditureCount(BankExpenditure bankExpenditure) {
        return bankExpenditureMapper.selectBankExpenditureCount(bankExpenditure);
    }

    /**
     * 新增消费
     *
     * @param bankExpenditure 消费
     * @return 结果
     */
    @Override
    public int insertBankExpenditure(BankExpenditure bankExpenditure) {
        return bankExpenditureMapper.insertBankExpenditure(bankExpenditure);
    }

    /**
     * 修改消费
     *
     * @param bankExpenditure 消费
     * @return 结果
     */
    @Override
    public int updateBankExpenditure(BankExpenditure bankExpenditure) {
        return bankExpenditureMapper.updateBankExpenditure(bankExpenditure);
    }

    /**
     * 删除消费对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBankExpenditureByIds(String ids) {
        return bankExpenditureMapper.deleteBankExpenditureByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除消费信息
     *
     * @param serial 消费ID
     * @return 结果
     */
    @Override
    public int deleteBankExpenditureById(Long serial) {
        return bankExpenditureMapper.deleteBankExpenditureById(serial);
    }

    /**
     * 查看签购单
     *
     * @return
     */
    @Override
    public Map selectSign(String serial) throws Exception {

        return bankExpenditureMapper.selectSign(serial);
    }

    /**
     * 根据流水号查询签名文件是否存在
     *
     * @param serial
     * @return
     * @throws Exception
     */
    @Override
    public Map selectSignEx(String serial) throws Exception {

        return (Map) bankExpenditureMapper.selectSignEx(serial);
    }

    /**
     * 添加签名图片
     *
     * @param map
     * @return
     */
    @Override
    public int insertsignpic(Map map) throws Exception {

        return bankExpenditureMapper.insertsignpic(map);
    }

    /**
     * 根据流水号查询Mpos信息
     *
     * @param serial
     * @return
     * @throws Exception
     */
    @Override
    public String selectMposBySerial(String serial) throws Exception {

        return (String) bankExpenditureMapper.selectMposBySerial(serial);
    }

    @Override
    public AjaxResult exportDetailExcel(BankExpenditure bankExpenditure) throws Exception {
        try {
            //bankExpenditure.setMerType("5");
            List<Map<String, Object>> list = bankExpenditureMapper.selectBankExpenditureDet(bankExpenditure);
            if (CollectionUtils.isEmpty(list)) {
                throw new BusinessException("导出数据不能为空");
            }

            if (list.size() > 200000) {
                throw new BusinessException("导出数据不能超过200000");

            }
            Map<String, List<Map<String, Object>>> exportMap = Maps.newHashMap();
            String time = null;
            //组装数据
            for (Map<String, Object> detail : list) {
                time = String.valueOf(detail.get("LOCALDATE")) + String.valueOf(detail.get("LOCALTIME"));
                detail.put("time",time);
                if (detail.get("MSGTYPE") != null) {
                    switch (detail.get("MSGTYPE").toString()) {
                        case "H007":
                            detail.put("MSGTYPE", "消费");
                            break;
                        case "H014":
                            detail.put("MSGTYPE", "退货");
                            break;
                        case "S007":
                            detail.put("MSGTYPE", "SIM卡年费");
                            break;
                        case "V007":
                            detail.put("MSGTYPE", "会员交易");
                            break;
                        case "C007":
                            detail.put("MSGTYPE", "手机充值");
                            break;
                        default:
                            detail.put("MSGTYPE", "未知");
                            break;
                    }
                }

                if (detail.get("STATUS") != null) {
                    switch (detail.get("STATUS").toString()) {
                        case "-1":
                            detail.put("STATUS", "撤销");
                            break;
                        case "-2":
                            detail.put("STATUS", "冲正");
                            break;
                        case "0":
                            detail.put("STATUS", "成功");
                            break;
                        case "2":
                            detail.put("STATUS", "失败");
                            break;
                        case "1":
                            detail.put("STATUS", "补录");
                            break;
                        default:
                            detail.put("STATUS", "未知");
                            break;
                    }
                }

                if (detail.get("CARDTYPE") != null) {
                    switch (detail.get("CARDTYPE").toString()) {
                        case "0":
                            detail.put("CARDTYPE", "未知");
                            break;
                        case "1":
                            detail.put("CARDTYPE", "借记卡");
                            break;
                        case "2":
                            detail.put("CARDTYPE", "贷记卡");
                            break;
                        case "3":
                            detail.put("CARDTYPE", "预付费卡");
                            break;
                    }
                }

                if (detail.get("SETT_TYPE") != null) {
                    switch (detail.get("SETT_TYPE").toString()) {
                        case "0":
                            detail.put("SETT_TYPE", "D0");
                            break;
                        case "1":
                            detail.put("SETT_TYPE", "T1");
                            break;
                    }
                }

                if (detail.get("AU_STATE") != null) {
                    switch (detail.get("AU_STATE").toString()) {
                        case "0":
                            detail.put("AU_STATE", "未清算");
                            break;
                        case "1":
                            detail.put("AU_STATE", "已清算");
                            break;
                    }
                }

                if (detail.get("ISYSF") != null) {
                    switch (detail.get("ISYSF").toString()) {
                        case "0":
                            detail.put("ISYSF", "否");
                            break;
                        case "1":
                            detail.put("ISYSF", "是");
                            break;
                    }
                }

                if (detail.get("ISDISSMIS") != null) {
                    switch (detail.get("ISDISSMIS").toString()) {
                        case "0":
                            detail.put("ISDISSMIS", "否");
                            break;
                        case "1":
                            detail.put("ISDISSMIS", "是");
                            break;
                    }
                }

            }
            LinkedHashMap<String, String> titleMap = new LinkedHashMap<>();
            titleMap.put("SERIAL", "流水号");
            titleMap.put("MER_NO", "商户编号");
            titleMap.put("LD_MERNO", "上游商户编号");
            titleMap.put("MER_NAME", "商户名称");
            titleMap.put("AGENT_NO", "服务商编号");
            titleMap.put("AGENT_NAME", "服务商名称");
            titleMap.put("SCREEN_NAME", "结算人");
            titleMap.put("MSGTYPE", "交易类型");
            titleMap.put("ISYSF", "是否云闪付");
            titleMap.put("ISDISSMIS", "是否双免");
            titleMap.put("TERNO", "终端号");
            titleMap.put("LOCALDATE", "交易时间");
            titleMap.put("PAN", "交易卡号");
            titleMap.put("AMOUNT", "交易金额(元)");
            titleMap.put("STATUS", "交易状态");
            titleMap.put("CARDTYPE", "卡类型");
            titleMap.put("SETT_TYPE", "清算类型");
            titleMap.put("AU_STATE", "清算状态");
            titleMap.put("RC", "返回码");
            String fileName = "交易明细数据" + DateUtils.dateTimeNow() +".xlsx";
            ExportExcelUtil.exportExcel(fileName, list, titleMap, "交易明细数据");
            return AjaxResult.success(fileName);

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<Map<String, String>> selectOnLineList(Map<String, String> reqMap) {
        return bankExpenditureMapper.selectOnLineList(reqMap);
    }

    @Override
    public Map selectSignatrue(String serial) {
        return bankExpenditureMapper.selectSignatrue(serial);
    }
}
